function f = LL_MTO_simplest(x)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%THIS FILE IS THE LIKELIHOOD FOR A SIMPLE ORDERED PROBIT MODEL 
%USED TO FIND A STARTING VALUE FOR THE FULL MODEL ESTIMATED IN 
%"Evidence of Neighborhood Effects from Moving to Opportunity: 
%     LATEs of Neighborhood Quality"
%by Dionissi Aliprantis and Francisca G.-C. Richter
%SEE THE INCLUDED readme.txt FILE FOR AN EXPLANATION OF THIS FILE 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%IMPORTING DATA AND LABELING VARIABLES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent data;
if isempty(data)
    data=importdata('C:\MTO_1\matlab\matlab_data.txt');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DROP OBSERVATIONS WITH ANY MISSING VARIABLES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data( any(data==-9,2), : ) = [];

%%%%%%%%%%%%%%%%
%LABEL VARIABLES
%%%%%%%%%%%%%%%%
D10         =data(:,1);     %D \in {1, 2, ..., 10}
HHnoteens   =data(:,2);     %X_1 
HHnofam     =data(:,3);     %X_2 
HHvictim    =data(:,4);     %X_3 
basequal    =data(:,5);     %X_4 
sitedummy2  =data(:,6);     %BOSTON  SITE DUMMY
sitedummy3  =data(:,7);     %CHICAGO SITE DUMMY
sitedummy4  =data(:,8);     %LA      SITE DUMMY
sitedummy5  =data(:,9);     %NYC     SITE DUMMY
exp         =data(:,10);    %INDICATOR FOR MTO EXPERIMENTAL VOUCHER 
S8          =data(:,11);    %INDICATOR FOR SECTION 8 VOUCHER
weight      =data(:,12);    %WEIGHTS
mover       =data(:,13);    %INDICATOR FOR MOVE WITH VOUCHER

N=length(D10);              %NUMBER OF OBSERVATIONS
T=2;                        %NUMBER OF TYPES (t=1 for S8, t=2 for Experimental)

C=zeros(N,9);               %COST CUTOFFS
gamma8=zeros(N,9);          %EFFECT OF VOUCHER INCLUDING UNOBSERVED HETEROGENEITY
gamma1=zeros(N,9);          %EFFECT OF VOUCHER INCLUDING UNOBSERVED HETEROGENEITY
index=zeros(N,9);           %LATENT INDEX

Pr=ones(N,1);               %PROBABILITY OF OBSERVATION i
LL=ones(N,1);               %LOG-LIKELIHOOD CONTRIBUTION OF OBSERVATION i

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CREATING LATENT INDEX COMPONENT BY COMPONENT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%
%mu(X)
%%%%%%
bHHnoteens   =x(1);
bHHnofam     =x(2);
bHHvictim    =x(3);
bbasequal    =x(4);
bsitedummy2  =x(5);
bsitedummy3  =x(6);
bsitedummy4  =x(7);
bsitedummy5  =x(8);

mu=zeros(N,1);

mu(:)=bHHnoteens*HHnoteens(:) +bHHnofam*HHnofam(:) +bHHvictim*HHvictim(:) +bbasequal*basequal(:) ...
    +bsitedummy2*sitedummy2(:) +bsitedummy3*sitedummy3(:) +bsitedummy4*sitedummy4(:) +bsitedummy5*sitedummy5;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%COST FUNCTION (ie, CUTOFFS)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bcutpoint(2)   =x(9);
bcutpoint(3)   =x(10);
bcutpoint(4)   =x(11);
bcutpoint(5)   =x(12);
bcutpoint(6)   =x(13);
bcutpoint(7)   =x(14);
bcutpoint(8)   =x(15);
bcutpoint(9)   =x(16);
bcutpoint(10)   =x(17);

C(:,1)=-Inf;
for j=2:10
    C(:,j)=bcutpoint(j);
end
C(:,11)=Inf;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LATENT INDEX OF INDIVIDUAL n AT LEVEL j 
%     (mu - C_j - V)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:11
    index(:,j)=mu(:)-C(:,j);
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%CREATING LOG-LIKELIHOOD FUNCTION
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PROBABILITY OF OBSERVATION n CONDITIONAL ON PARAMETERS
%Pr(D=j) = F_V(mu - C_{j}) - F_V(mu - C_{j+1})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Prob=zeros(N,10);
    for j=1:10
            Prob(:,j)=double(normcdf(index(:,j))-normcdf(index(:,j+1))).*(D10(:) == j); 
    end   

Pr=zeros(N,1);    
    for j=1:10
            Pr(:)=Pr(:)+Prob(:,j).*(D10(:) == j);
    end

LL(:)=double(log(max(0.0000001,Pr(:)))).*weight(:);

f=double(-sum(LL(:)));
